#include "yb_led.h"
#include "crc.h"
#include "QDateTime"
/*＊*****************************
入口： line 行号
      DeviceType   卡类型
      info         显示的数据
      resBuf       返回的数据
*/
void yb_led_create_park(unsigned char line,DeviceType_t DeviceType,QString info,QByteArray *resBuf)
{
    QByteArray buffer;
    QTextCodec *gb2312 = QTextCodec::codecForName("GB2312");
    QByteArray infBuffer = gb2312->fromUnicode(info);
    //PHP0
    char PHY0[] =    {
                            0x01,0x00,//dstAddr
                            0x00,0x80, //srcAddr
                            0x00,0x00,0x00,0x00,0x00,//reserved
                            0x00,    //display mode
                            0x00,    //device type
                            0x02,    //protocolVersion
                            0x00,0x00,//dataLen
                        };
    PHY0[10] = (unsigned char)DeviceType;
    PHY0[12] = (unsigned char)((infBuffer.length()+36)%256);
    PHY0[13] = (unsigned char)((infBuffer.length()+36)/256);
    char PHY1[] = {0xA3,0x06,0x02,0x00,0x00,0x00,
                         0x01,  //areaNum
                         0x00,0x00 //areaDataLen
                        };
    PHY1[7] = (unsigned char)((infBuffer.length()+27)%256);
    PHY1[8] = (unsigned char)((infBuffer.length()+27)/256);
    char areaData[] = {0x00,
                             0x00,0x00, //area X
                             0x00,0x00, //area Y
                             0x08,0x00, //area width
                             0x10,0x00, //area height
                             0x00,      //DynamicAreaLoc
                             0x00,      //Lines sizes
                             0x00,      //run mode
                             0x0A,0x00, //timeout S
                             0x00,0x00,0x00,    //reserved
                             0x01,      //sigleLine
                             0x01,      //newLine
                             0x00,      //DisplayMode
                             0x00,      //exitMode
                             0x06,      //speed
                             0x00,      //stayTime
                             0x00,0x00,0x00,0x00  //dataLen
                         };

    areaData[3] = (unsigned char)((line-1)*16); //area Y
    areaData[9] = (unsigned char)(line-1); //area Y
    if(infBuffer.length()>8)
    {
        if(info.contains("\\D")!=-1)
        {
             areaData[19] = LEFT_MODE;
        }else
        {
            areaData[19] = STATIC_MODE;
        }
    }
    else
    {
       areaData[19] = STATIC_MODE;
    }
    areaData[23] = (unsigned char)(infBuffer.length()%256);
    areaData[24] = (unsigned char)(infBuffer.length()/256);
    buffer.append(PHY0,sizeof(PHY0));
    buffer.append(PHY1,sizeof(PHY1));
    buffer.append(areaData,sizeof(areaData));
    buffer.append(infBuffer);
    short unsigned int crc = CalcCRC((unsigned char *)buffer.data(),(unsigned int)buffer.length());
    char header[] = {0xA5,0xA5,0xA5,0xA5,0xA5,0xA5,0xA5,0xA5};
    resBuf->append(header,sizeof(header));
    resBuf->append(buffer);
    resBuf->append((unsigned char)(crc%256));
    resBuf->append((unsigned char)(crc/256));
    resBuf->append((unsigned char)0x5A);
}
/*＊*****************************
入口： line 行号
      DisplayMode  显示模式
      DeviceType   卡类型
      info         显示的数据
      resBuf       返回的数据
*/
void yb_led_syncTime(QByteArray *resBuf)
{
    QByteArray buffer;
    //PHP0
    char PHY0[] =    {
                            0x01,0x00,//dstAddr
                            0x00,0x80, //srcAddr
                            0x00,0x00,0x00,0x00,0x00,//reserved
                            0x00,    //display mode
                            0x51,    //device type
                            0x02,    //protocolVersion
                            0x0D,0x00,//dataLen
                        };
    char PHY1[] = {0xA2,0x03,0x02,0x00,0x00,
                   0x16,0x20,  //年
                   0x01,       //月
                   0x01,       //日
                   0x01,       //时
                   0x11,       //分
                   0x11,       //秒
                   0x01       //星期
                    };

    QDateTime clock = QDateTime::currentDateTime();
    int valueH = clock.date().year()/100;
    int valueL = clock.date().year()%100;
    PHY1[5] =(unsigned char)((valueL/10)*16 + valueL%10);
      qDebug()<<"valueL"<<(unsigned char)((valueL/10)*16 + valueL%10);
    PHY1[6] = (unsigned char)((valueH/10)*16 + valueH%10);
      qDebug()<<"valueH"<<(unsigned char)((valueH/10)*16 + valueH%10);
    PHY1[7] = (unsigned char)((clock.date().month()/10)*16 + clock.date().month()%10);
    PHY1[8] = (unsigned char)((clock.date().day()/10)*16 + clock.date().day()%10);
    PHY1[9] = (unsigned char)((clock.time().hour()/10)*16 + clock.time().hour()%10);
    PHY1[10] = (unsigned char)((clock.time().minute()/10)*16 + clock.time().minute()%10);
    PHY1[11] = (unsigned char)((clock.time().second()/10)*16 + clock.time().second()%10);
    PHY1[12] = (unsigned char)((clock.time().second()/10)*16 + clock.time().second()%10);
    PHY1[13] = (unsigned char)clock.date().weekNumber();
    buffer.append(PHY0,sizeof(PHY0));
    buffer.append(PHY1,sizeof(PHY1));
    short unsigned int crc = CalcCRC((unsigned char *)buffer.data(),(unsigned int)buffer.length());
    char header[] = {0xA5,0xA5,0xA5,0xA5,0xA5,0xA5,0xA5,0xA5};
    resBuf->append(header,sizeof(header));
    resBuf->append(buffer);
    resBuf->append((unsigned char)(crc%256));
    resBuf->append((unsigned char)(crc/256));
    resBuf->append((unsigned char)0x5A);
}
